package com.chenwb.threadPool;

import java.util.concurrent.*;

public class ThreadPoolDemo implements Runnable {
	private static ThreadPoolExecutor baseTpe;
	private static ThreadPoolExecutor additionTpe;
	private int time;

	public ThreadPoolDemo(int time) {
		this.time = time;
	}

	static {
		baseTpe = new ThreadPoolExecutor(1000, 2000, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
		additionTpe = new ThreadPoolExecutor(5, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(200));
	}


	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "正在执行");
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 创建插入的任务
	 */
	private static final class addtionTask implements Runnable {

		@Override
		public void run() {
			System.out.println(Thread.currentThread().getName() + "正在执行");
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		Thread[] threads = new Thread[1000];
		for (int i = 0; i < threads.length; i++) {
			threads[i] = new Thread(new ThreadPoolDemo(1), "Thread" + i);
		}
		for (Thread thread : threads) {
			baseTpe.execute(thread);
		}

		Thread[] threads1 = new Thread[10000];
		for (int i = 0; i < threads1.length; i++) {
			threads1[i] = new Thread(new addtionTask(), "Thread" + i);
		}
		for (Thread thread : threads1) {
			additionTpe.execute(thread);
		}

		Thread[] threads2 = new Thread[1000];
		for (int i = 0; i < threads.length; i++) {
			threads2[i] = new Thread(new ThreadPoolDemo(10000), "Thread" + i);
		}
		for (Thread thread : threads2) {
			baseTpe.execute(thread);
		}
	}

}
